package com.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.annotation.ControllerEndpoint;
import com.hotel.entity.Role;
import com.hotel.enums.RoleStatusEnum;
import com.hotel.response.Result;
import com.hotel.service.MenuService;
import com.hotel.service.RoleService;
import com.hotel.vo.MenuNodeVO;
import com.hotel.vo.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author XiaoGe
 * @since 2022-05-28
 */
@Api(tags = "系统角色接口")
@CrossOrigin
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @Autowired
    private MenuService menuService;


    /**
     * 角色授权
     *
     * @param mids
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "角色授权失败", operation = "角色授权")
    @ApiOperation(value = "角色授权")
    //@RequiresPermissions({"role:authority"})
    @PostMapping("/authority/{id}")
    public Result authority(@PathVariable Long id, @RequestBody Long[] mids) {
        roleService.authority(id, mids);
        return Result.ok();
    }

    /**
     * 角色拥有的菜单权限id和菜单树
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "角色菜单")
    @GetMapping("/getRoleMenu/{id}")
    public Result getRoleMenu(@PathVariable Long id) {
        List<MenuNodeVO> tree = menuService.findMenuTree();
        //角色拥有的菜单id
        List<Long> mids = roleService.findMenuIdsByRoleId(id);
        List<Long> ids = menuService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", tree);
        map.put("mids", mids);
        map.put("open", ids);
        return Result.ok().data(map);
    }

    /**
     * 角色列表
     *
     * @return
     */
    @ApiOperation(value = "角色列表")
    @GetMapping("/getRoleList")
    public Result getRoleList(@RequestParam(value = "current",required = true ,  defaultValue = "1") Integer current,
                               @RequestParam(value = "size",required = true ,  defaultValue = "7") Integer size,
                               @RequestParam(value = "roleName", required = false ) String roleName) {
        Page<Role> page = new Page<>(current,size);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(roleName),Role::getRoleName,roleName);
        Page<Role> rolePage = roleService.page(page, wrapper);
        return Result.ok().data("rolePage",rolePage);
    }

    /**
     * 添加角色信息
     *
     * @param role
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加角色失败", operation = "添加角色")
    @ApiOperation(value = "添加角色")
    @RequiresPermissions({"role:add"})
    @PostMapping("/add")
    public Result add(@RequestBody Role role) {
        role.setStatus(RoleStatusEnum.AVAILABLE.getStatusCode());
        role.setCreateTime(new Date());
        role.setModifiedTime(new Date());
        roleService.save(role);
        return Result.ok();
    }

    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除角色失败", operation = "删除角色")
    @ApiOperation(value = "删除角色", notes = "根据id删除角色信息")
    @RequiresPermissions({"role:delete"})
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        roleService.delete(id);
        return Result.ok();
    }

    /**
     * 编辑角色信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑用户", notes = "根据id更新角色信息")
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"role:edit"})
    public Result edit(@PathVariable Long id) {
        RoleVO roleVO = roleService.edit(id);
        return Result.ok().data("role",roleVO);
    }

    /**
     * 更新角色
     *
     * @param roleVO
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新角色失败", operation = "更新角色")
    @ApiOperation(value = "更新角色", notes = "根据id更新角色信息")
    @RequiresPermissions({"role:update"})
    @PutMapping("/update")
    public Result update(@RequestBody RoleVO roleVO) {
        System.out.println(roleVO.toString());
        roleService.updateRole(roleVO);
        return Result.ok();
    }

    /**
     * 更新角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "角色更新状态失败", operation = "角色|禁用/启用")
    @ApiOperation(value = "更新状态", notes = "禁用和更新两种状态")
    @RequiresPermissions({"role:status"})
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Boolean status) {
        roleService.updateStatus(id, status);
        return Result.ok();
    }
}
